package cn.wp.common.core.plugin.fileserver;

import cn.wp.common.core.exception.WpServiceException;
import cn.wp.common.core.plugin.fileserver.strategy.DateStrategy;
import cn.wp.common.core.plugin.fileserver.strategy.TopicStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 文件服务配置
 * Created by chenyb on 2019/09/30
 */
@Configuration
public class FileServerConfiguration {

    @Autowired
    private FileServerProperties fileServerProperties;

    @Bean
    DateStrategy dateStrategy(){
        return new DateStrategy();
    }

    @Bean
    TopicStrategy topicStrategy(){
        return new TopicStrategy();
    }

    @Bean
    StorageStrategy storageStrategy(){
        FileInfo fileInfo = null;
        if("local".equals(fileServerProperties.getType())){
            if(fileServerProperties.getStorageStrategy().startsWith("date")){
                return new DateStrategy();
            }

            if("topic".equals(fileServerProperties.getStorageStrategy())){
                return new TopicStrategy();
            }
        }

        if(fileInfo == null){
            throw new WpServiceException("文件服务器配置[ fileserver.type ]未找到合适的type值");
        }

        return null;
    }
}
